package com.cskaoyan.controller.backStage;

import com.cskaoyan.model.Storage;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.StorageListDataVO;
import com.cskaoyan.model.vo.StorageVO;
import com.cskaoyan.service.StorageService;
import com.cskaoyan.util.FileProcessUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @program: mall38
 * @description: 对象存储模块
 * @author: Lexis
 * @create: 2022-05-05 19:55
 **/
@RestController
@RequestMapping("admin/storage")
public class StorageController {

    @Autowired
    StorageService storageService;
    @RequiresPermissions("admin:storage:create")
    @PostMapping("create")
    /**
     * @author: lixiaosheng
     * @description: 上传文件
     * @date: 2022/5/5 20:22
     */
    public BaseRespVO<StorageVO> create(MultipartFile file){
        StorageVO fileInfo = FileProcessUtil.getFileInfo(file);
        try {
            file.transferTo(new File("D:\\JavaWorkspace\\tmp",fileInfo.getKey()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        fileInfo.setAddTime(new Date());
        storageService.create(fileInfo);
        return BaseRespVO.ok(fileInfo);
    }

    /**
     * @author: wlyq
     * @description: 文件列表
     * @date: 2022/5/6 14:22
     */
    @RequiresPermissions("admin:storage:list")
    @GetMapping("list")
    public BaseRespVO<StorageListDataVO> storageList(BaseParamBO baseParamBO){
        BaseRespVO<StorageListDataVO> storageListDataVOBaseRespVO = storageService.storgeList(baseParamBO);
        return storageListDataVOBaseRespVO;
    }

    /**
     * @author: wlyq
     * @description: 文件列表修改
     * @date: 2022/5/6 15:12
     */
    @RequiresPermissions("admin:storage:update")
    @PostMapping("update")
    public BaseRespVO<Storage> storgeUpdate(@RequestBody Storage storage){
        BaseRespVO<Storage> storageBaseRespVO = storageService.storageUpdate(storage);
        return storageBaseRespVO;
    }

    /**
     * @author: wlyq
     * @description: 文件列表逻辑删除
     * @date: 2022/5/7 09：27
     */
    @RequiresPermissions("admin:storage:delete")
    @PostMapping("delete")
    public BaseRespVO storageDelete(@RequestBody Storage storage){
        BaseRespVO baseRespVO = storageService.storgeDelete(storage);
        return baseRespVO;
    }
}
